package com.example.bilibili.controller;

import com.example.bilibili.entity.Breply;
import com.example.bilibili.service.BreplyService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Breply)表控制层
 *
 * @author makejava
 * @since 2022-08-16 20:29:29
 */
@RestController
@RequestMapping("breply")
@CrossOrigin(origins = "*")
public class BreplyController {
    /**
     * 服务对象
     */
    @Resource
    private BreplyService breplyService;

    /**
     * 分页查询
     *
     * @param breply 筛选条件
     * @return 查询结果
     */
    @GetMapping("getList")
    public ResponseEntity<Page<Breply>> queryByPage(Breply breply, 
                                                    @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size,
                                                    @RequestParam(defaultValue = "id") String sort, boolean asc) {
        PageRequest pageRequest = PageRequest.of(page, size, asc ? Sort.Direction.ASC : Sort.Direction.DESC, sort);
        return ResponseEntity.ok(this.breplyService.queryByPage(breply, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getOne")
    public ResponseEntity<Breply> queryById( String id) {
        return ResponseEntity.ok(this.breplyService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param breply 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    public ResponseEntity<Breply> add(Breply breply) {
        return ResponseEntity.ok(this.breplyService.insert(breply));
    }

    /**
     * 编辑数据
     *
     * @param breply 实体
     * @return 编辑结果
     */
    @PutMapping("update")
    public ResponseEntity<Breply> edit(Breply breply) {
        return ResponseEntity.ok(this.breplyService.update(breply));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.breplyService.deleteById(id));
    }
}

